import http from "./http.js"

import config from "../config/index.js"

import request from "./Request.js"

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro

http.initConfig({
	baseURL: baseUrl,
	timeout: 10000
})

//请求拦截
http.interceptors.request.use((config) => {

	try {
		const token = uni.getStorageSync('hmc-token');
		if (token) {
			options.header = {
				'Authorization': token
			};
		}
	} catch (err) {
		console.log("获取Token：", err)
	}

	return Promise.resolve(config)
})


//响应拦截
http.interceptors.response.use((res) => {
	const {
		data,
		statusCode
	} = res || {};

	if (statusCode == 200) {
		// let data = JSON.parse(data);
		if (!(data.length > 0 || Object.keys(data).length > 0) && !data) {
			uni.showToast({
				title: "暂无数据",
				icon: "none"
			})
		}
		return Promise.resolve(data)
	} else {
		return Promise.reject("HTTP:状态码异常!");
	}
})

const Request = new request(baseUrl)
export default Request